<?php
require_once '../libraries/utilites/functions.php';
$path = "../ptnhuan-web1/data/films.txt";
$full_path = realpath($path); //doc duong dan truoc no : C://...
$films = mreadFile($path);
$rating = NULL;
$rating1 = NULL;
?>
<link rel="stylesheet" type="text/css" href="public/css/style.css">
 Hom Nay : <?php echo date("d/m/Y")."<br>"; ?>
<form method="POST">
    <table>
        <tr>
       <td>From</td> <td><input type="text" name="rating" placeholder="from"></td><tr>
   <tr><td> To</td> <td><input type="text" name="rating1" placeholder="to"></td>
   <td> <input type="submit" name="submit" value="Search"><td><tr>
   <p class="viena"></p>
   <tr><td> Usersname</td> <td><input type="text" name="username"></td></tr>
   <tr> <td>Password</td> <td><input type="password" name="password"></td>
    <td><input type="submit" name="submits" value="Login"></td></tr>
    </table>
</form>
<?php if (isset($_POST['submit']))://isset function kiem tra co ja tri..? ?>
    <?php $rating = (double) /* khai vao */ $_POST['rating']; ?>
    <?php $rating1 = (double) $_POST['rating1']; ?>
<?php endif; ?>
<?php foreach ($films as $item): ?>
    <?php
    $b= trim($item,"\n\t\r ");//cat ja tri nhung dau
    $film = explode(";", $item);
   if($b!="")//neu no la "" thi thuc hien
   {
    //kjem tra hinh co dau cach
    ?>

    <?php if (empty($rating) && empty($rating1)): ?>
        <?php if (isset($film[2]) || isset($film[3])): ?>
            <a href = '../ptnhuan-web1/public/images/films/<?php echo $film[1] ?>'>
                <img src = '../ptnhuan-web1/public/images/films/<?php echo $film[1]; ?>'> </a>
            <?php echo $film[2]; ?>
            <?php echo $film[3]; ?>
            <a href="edit.php?if=<?php echo $film[0]; ?>">Edit</a></br>                    
        <?php endif; ?>
    <?php else: ?>
        <?php if (isset($film[2]) || isset($film[3])): ?>
            <?php if (($rating <= (double) $film[3]) && ($rating1 >= (double) $film[3])): ?>
                <a href = '../ptnhuan-web1/public/images/films/<?php echo $film[1] ?>'>
                <img src = '../ptnhuan-web1/public/images/films/<?php echo $film[1]; ?>'> </a>
            <?php echo $film[2]; ?>
            <?php echo $film[3]; ?>
            <a href="edit.php?if=<?php echo $film[0]; ?>">Edit</a></br>  
            <?php endif; ?>
        <?php endif; ?>
    <?php endif; ?>
   <?php }?>
<?php endforeach; ?>
<?php if (isset($_POST['submits'])): ?>
    <?php
     $usersname = $_POST['username'];//kiem tra xem da nhap tren form login chua
     $password  = $_POST['password'];
     $isOk = FALSE;//la hien tai la nut submit chua dang nhap vao la false,dc la true
     $index = -1;//hien tai cho la -1;tuong ung voi cai chi so trong array
     //Kiem tra user
     for($i = 0;$i < count($users);$i ++){
         if((strcmp($users[$i]['username'],$usersname) == 0 ) &&
         (strcmp($users[$i]['password'],$password) == 0)){
             $isOk = TRUE;
             $index = $i;
         }
     }
     //Kiem Tra Dang Nhap
     if($isOk){
         //Dang Nhap Thanh Cong
         header("Location: http://localhost:82/nhathien-web1/film.php?uid=".$users[$index]['id']);
     }
 else {
     echo "Dang Nhap That Bai"  ;  
     }
?>
     <?php endif; ?>